package cn.edu.scnu.semart.goods.controller;

import cn.edu.scnu.semart.model.goods.Category;
import cn.edu.scnu.semart.goods.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品分类Controller
 */
@RestController
@RequestMapping("/category")
//@CrossOrigin
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 获取所有分类
     */
    @GetMapping("/list")
    public Map<String, Object> getAllCategories() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Category> categories = categoryService.getAllCategories();
            result.put("success", true);
            result.put("data", categories);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 获取分类树
     */
    @GetMapping("/tree")
    public Map<String, Object> getCategoryTree() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Category> categories = categoryService.getCategoryTree();
            result.put("success", true);
            result.put("data", categories);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 根据父分类ID获取子分类
     */
    @GetMapping("/children/{parentId}")
    public Map<String, Object> getCategoriesByParentId(@PathVariable Integer parentId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Category> categories = categoryService.getCategoriesByParentId(parentId);
            result.put("success", true);
            result.put("data", categories);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 添加分类
     */
    @PostMapping("/add")
    public Map<String, Object> addCategory(@RequestBody Category category) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = categoryService.addCategory(category);
            result.put("success", success);
            if (success) {
                result.put("message", "添加成功");
            } else {
                result.put("message", "添加失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 更新分类
     */
    @PutMapping("/update")
    public Map<String, Object> updateCategory(@RequestBody Category category) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = categoryService.updateCategory(category);
            result.put("success", success);
            if (success) {
                result.put("message", "更新成功");
            } else {
                result.put("message", "更新失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 删除分类
     */
    @DeleteMapping("/delete/{id}")
    public Map<String, Object> deleteCategory(@PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = categoryService.deleteCategory(id);
            result.put("success", success);
            if (success) {
                result.put("message", "删除成功");
            } else {
                result.put("message", "删除失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }
} 